<?php
/*
 * Created on 2011-1-15
 * by lixilei
 */

class ConnectCtrl extends spController{
	
	/**
	 * 建立同步关系
	 */
	public function connect(){
		// 获取参数
		$user_id = $_SESSION["userInfo"]["user_id"];
		$email = trim($_POST["email"]);
		$password = trim($_POST["password"]);
		$web = trim($_REQUEST["web"]);
		
		if($email == NULL || $password == NULL){
			$this->results = $_SESSION;
			$this->results += array("ret"=>_FAIL, "errMsg"=>_CONNECT_EMAIL_OR_PWD_NULL);
			// 记录并调度信息
			$toURL = "connect-".$web.".htm";
			$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
			$dispatchBiz->recordDispatch($toURL, "ConnectCtrl&connect&".$web."&error&argsNull", $user_id);
			$this->display($toURL);
			return;
		}
		
		// 执行业务
		switch ($web){
			case "renren":
				$renrenBiz = spClass("RenRenMobileBiz", null, BIZ_PATH."RenRenMobileBiz.php");
				$result = $renrenBiz->setConnectionInfo($user_id, $email, $password);
				// 结果处理
				if($result["ret"] == _SUCCESS){
					// 更新session信息
					$userBiz = spClass("UserBiz", null, BIZ_PATH."UserBiz.php");
					$userInfo = $userBiz->freshUserInfo($user_id);
					$_SESSION["userInfo"] = $userInfo;
					$this->results = $_SESSION;
					// 记录并调度信息
					$toURL = "connection-list.htm";
					$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
					$dispatchBiz->recordDispatch($toURL, "ConnectCtrl&connect&".$web, $user_id);
					$this->display($toURL);
				} else{
					$this->results = $_SESSION;
					$this->results += $result;
					// 记录并调度信息
					$toURL = "connect-".$web.".htm";
					$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
					$dispatchBiz->recordDispatch($toURL, "ConnectCtrl&connect&".$web."&failed", $user_id);
					$this->display($toURL);
				}
				break; 
			default:
				$this->results = $_SESSION;
				// 记录并调度信息
				$toURL = "connection-list.htm";
				$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
				$dispatchBiz->recordDispatch($toURL, "ConnectCtrl&connect&default", $user_id);
				$this->display($toURL);
				break;
		}
	}
	
	/**
	 * 取消同步关系
	 */
	public function disconnect(){
		// 获取参数
		$user_id = $_SESSION["userInfo"]["user_id"];
		$web = trim($_REQUEST["web"]);
		
		// 执行业务
		switch ($web){
			case "renren":
				$renrenBiz = spClass("RenRenMobileBiz", null, BIZ_PATH."RenRenMobileBiz.php");
				$result = $renrenBiz->disconnect($user_id);
				// 结果处理
				if($result){
					// 更新session信息
					$userBiz = spClass("UserBiz", null, BIZ_PATH."UserBiz.php");
					$userInfo = $userBiz->freshUserInfo($user_id);
					$_SESSION["userInfo"] = $userInfo;
					$this->results = $_SESSION;
					// 记录并调度信息
					$toURL = "connection-list.htm";
					$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
					$dispatchBiz->recordDispatch($toURL, "ConnectCtrl&disconnect&".$web, $user_id);
					$this->display($toURL);
				} else{
					$this->results = $_SESSION;
					$this->results += array("ret"=>_FAIL, "errMsg"=>_RENREN_DISCONNECT_FAIL);
					// 记录并调度信息
					$toURL = "connection-list.htm";
					$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
					$dispatchBiz->recordDispatch($toURL, "ConnectCtrl&disconnect&".$web."&failed", $user_id);
					$this->display($toURL);
				}
				break; 
			default:
				$this->results = $_SESSION;
				// 记录并调度信息
				$toURL = "connection-list.htm";
				$dispatchBiz = spClass("DispatchBiz", null, BIZ_PATH."DispatchBiz.php");
				$dispatchBiz->recordDispatch($toURL, "ConnectCtrl&disconnect&default", $user_id);
				$this->display($toURL);
				break;
		}
	}
}
?>
